// pages/my_orders/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabIdx: 0,
        nowList:{  
            total: 0,
            current_page: 0,
            last_page: 1,
            data: []
          },
    },
    godetail(e){
    wx.navigateTo({
      url: '/pages/order_detail/index?order_id='+e.currentTarget.dataset.id,
    })
    },
    gorefund(e){
      wx.navigateTo({
        url: '/pages/refund/index?order_id='+e.currentTarget.dataset.id,
      })
    },
    tabClick(e) {
        this.setData({
            tabIdx: e.currentTarget.dataset.i,
        })
        this.init_Data()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        this.setData({
            tabIdx:options.tab
        })
        console.log(111)
        this.init_Data();
    },
    init_Data(){
        var that = this;
        that.setData({
          'nowList.current_page': 0,
      });
     that.getMyOrders()
    
    },
    getMyOrders(){
        var json = {},that=this;
        json.status=that.data.tabIdx;
        json.page = Number(that.data.nowList.current_page) + 1;
        if (json.page > 1 && that.data.nowList.current_page >= that.data.nowList.last_page) {       
            return false;
        }
        request.requestLoading(
            "/api/order/myorders",
            json,
            'POST',
          ).then((res) => {
            var nowList = res.data;
            that.setData({
                is_show:true
            })
    
            if (json.page > 1) {
                var now_nowList_data =that.data.nowList.data || [];
                var now_nowList_data = now_nowList_data.concat(nowList.data);
                nowList.data = now_nowList_data;
            }
      
            that.setData({
              nowList: nowList
            });
          
           
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    cancelOrder(e){
        var json={},that=this;
        json.order_id=e.currentTarget.dataset.id
        request.requestLoading(
            "/api/order/cancelOrder",
            json,
            'POST',
          ).then((res) => {
            if(res.code==1){
                
                that.init_Data()
               }else{
                   wx.showToast({
                     title: res.msg,
                     icon:'none'
                   })
               }
           
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    payOrder(e){
        var json={},that=this;
        json.order_id=e.currentTarget.dataset.id
        request.requestLoading(
            "/api/order/payNow",
            json,
            'POST',
          ).then((res) => {
            if(res.code==1){
                
                  if(res.data.is_pay==1){
                  
                    var wxconfig = res.data.wxconfig;
                    // 支付
                    wx.requestPayment({
                      timeStamp: wxconfig.timeStamp,
                      nonceStr: wxconfig.nonceStr,
                      package: wxconfig.package,
                      signType: wxconfig.signType,
                      paySign: wxconfig.paySign,
                      success() {
                       that.init_Data()
                      },
                      fail() {
                       wx.showToast({
                         title: '支付取消',
                         icon:'none'
                       })
                      }
                    })
                  }else{
                    that.setData({
                        buyPop:true
                    })
                  }
               }
           
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    gobenfits(){
      wx.navigateTo({
        url: '/pages/my_rights/index',
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        /**
         *  globalData: {
    isIPhone6: false, //当前设备是否为 iPhone 6
    userInfo: null,
    openid:null,
    is_show:0,
    mapSdkKey: '456BZ-M7VWL-CUNPQ-MHRZW-P2LUE-5SBLJ',
    mapSdkSk: 'XuUCHWTFLnjO0riCyxd0wafhXMIIPPeh',
    lat:'',
    lng:'',
    is_need_refresh:false,
         */
       if(getApp().globalData.is_need_refresh){
        getApp().globalData.is_need_refresh=false
           this.init_Data()
       }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.init_Data();
   
        setTimeout(function(){
          wx.stopPullDownRefresh();
        },500)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        var that =this;

        if (that.data.nowList.current_page >= that.data.nowList.last_page) {
          // functions.toast('已加载到底部了');
          return false;
      } else {
        that.getMyOrders();
      }
    },

   
})